package com.ww.yaodao.interceptor;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ww.yaodao.config.JWTConfig;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.SignatureException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class TokenInterceptor extends HandlerInterceptorAdapter {
    @Resource
    private JWTConfig jwtConfig;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

       /* String requestURI = request.getRequestURI();
        if (requestURI.contains("/login") || requestURI.contains("/register")) {
            log.info("进入登录或者注册...");
            return true;
        } else {
            try {
                String token = request.getHeader("Authorization") == null ? request.getHeader("Authorization") : null;
                log.info("token:{}", token);
                Map<String, Object> map = new HashMap<>();
                JWTConfig.verify(token);//验证令牌
                map.put("state",false);//设置状态
                String json = new ObjectMapper().writeValueAsString(map);
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().print(json);
                return true;//放行请求
            } catch (Exception e) {
                e.printStackTrace();
            }

        }*/
       /* String uri = request.getRequestURI() ;
        if (uri.contains("/login") || uri.contains("/register")){
            log.info("11111111111");
            return true;
        }
        String connection1 = request.getHeader("Authorization");
        log.info("Authorization1111111111111111111111111{}",connection1);
        if (connection1==null){
            throw new Exception("token为空");
        }
        Map<String,Object> map = new HashMap<>();
        try{
            JWTConfig.verify(connection1);//验证令牌
            return true;//放行请求
        } catch (SignatureVerificationException e) {
            e.printStackTrace();
            map.put("msg","无效签名");
            map.put("msg","无效签名");
        } catch (TokenExpiredException e) {
            e.printStackTrace();
            map.put("msg","token过期");
        } catch (AlgorithmMismatchException e) {
            e.printStackTrace();
            map.put("msg","token算法不一致");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","token失效");
        }
        map.put("state",false);//设置状态
        //将map转化成json，response使用的是Jackson
        String json = new ObjectMapper().writeValueAsString(map);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().print(json);*/
//        return true;
        return true;
    }
}
